package com.cyzy.controller;

import com.cyzy.dto.Result;
import com.cyzy.pojo.AdminUser;
import com.cyzy.pojo.Menu;
import com.cyzy.pojo.RoleMenu;
import com.cyzy.service.MenuService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menumanager")
public class MenuManagementController {
    //RESTful接口规范：查询用get  增加用post  修改用put  删除用delect
    //登录用post

    @Autowired
    private MenuService menuService;

    //对菜单管理进行分页
    @GetMapping("/page")
    public Result roleMenuPageHelper(int page,int size){
        // 启动分页，page表示当前页，size表示每页的记录数
//        PageHelper.startPage(page, size);
        PageHelper.startPage(page,size);


        // 获取分页后的数据
        List<Menu> MenuList = menuService.selectAllMenuList();
//        System.out.println(MenuList + " " + "7984987484648");
        if (MenuList.isEmpty()){
            return Result.error().message("暂无数据！");
        }

//        PageInfo<RoleMenu> pageInfo = new PageInfo<>(MenuList);
        PageInfo<Menu> pageInfo = new PageInfo<>(MenuList);
//        System.out.println(adminMenuList);
//        System.out.println(pageInfo);
// 返回分页后的数据列表
        return Result.ok().data("list", MenuList).data("count",pageInfo.getTotal());
        }

    //搜索模块的查询
    @GetMapping("/searchrolemenu")
    public Result searchrolemenu(Menu menu) {
        System.out.println(menu + "21621351351");
        // 处理参数，防止 SQL 语句出错
        if ((menu.getMenuName() == null || menu.getMenuName().isEmpty()) &&
                (menu.getMenuState() == null)) {
            return Result.error().message("请至少输入一个搜索条件");
        }

        List<Menu> menuList = menuService.searchMenuAll(menu.getMenuName(),
                menu.getMenuState());
        System.out.println(menuList + "163165463135");

        if (menuList != null && !menuList.isEmpty()) {
            PageInfo<Menu> pageInfo = new PageInfo<>(menuList);
            // 返回分页后的数据列表
            return Result.ok().data("list",menuList).data("count",pageInfo.getTotal());
        }

        return Result.error().message("未找到符合条件的菜单！");
    }

    //获取菜单列表
    @GetMapping("/getmenu")
    public Result getMenuAllList(){
        List<Menu> menuList = menuService.getMenuAllList();
        if (menuList == null || menuList.isEmpty()){
            return Result.error().message("获取失败！");
        }
        return Result.ok().data("list",menuList);
    }

//    对菜单进行添加
    @PostMapping("/add")
    public Result additionalManage(@RequestBody Menu menu){
//        System.out.println(adminUser + " " + "231843351481515");
        //新增之前，判断该菜单有无在数据库中存在
        Menu menu1 = menuService.selectMenu(menu.getMenuName());
        if (menu1 != null){
            return Result.error().message("该菜单已存在，请勿重复添加！");
        }

        int res = menuService.getBaseMapper().insert(menu);
//        System.out.println(res);
        if (res == 0){
            return Result.error().message("新增失败！");
        }
        return Result.ok().message("新增成功！");
    }

    //对菜单进行修改
    @PutMapping("/upd")
    public Result updateManage(@RequestBody Menu menu){
//        System.out.println(menu + " " + "262348513516848");
        int res = menuService.updateManage(menu.getMenuId(),menu.getMenuName(),menu.getPath(),menu.getMenuState(),
                menu.getName(),menu.getComponent(),menu.getIcon());
//        System.out.println(res + "1651315");
        if (res == 0){
            return Result.error().message("修改失败！");
        }
        return Result.ok().message("修改成功!");
    }

    //对菜单进行逻辑删除
    @DeleteMapping("/del")
    public Result deleteManage(@RequestBody Menu menu){
//        System.out.println(menu + "561535461");

        int res = menuService.deleteState(menu.getMenuId(),menu.getMenuState());
        System.out.println(res + "16484541");
        if(res != 1){
            return Result.error().message("删除失败！");
        }
        return Result.ok().message("删除成功！");
    }

    //对菜单进行逻辑批量删除
    @DeleteMapping("/dele")
    public Result deleteManage1(@RequestParam("menuState") String state,@RequestParam("menuId") List<String> ids){

        int res = menuService.deleteStateByIds(ids, state); // 调用批量逻辑删除方法
        System.out.println(res + "46416515");
        if (res == 0) {
            return Result.error().message("批量删除失败！");
        }
        return Result.ok().message("成功删除 " + res + " 条记录！");
    }
}
